package com.wms.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wms.bean.PageBean;
import com.wms.bean.ResultBean;
import com.wms.core.R;
import com.wms.dto.WarehouseAddDTO;
import com.wms.entity.Kuqu;
import com.wms.entity.Warehouse;
import com.wms.mapper.StoreAreaMapper;
import com.wms.mapper.StoreMapper;
import com.wms.service.StoreAreaService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

@Service
public class StoreAreaServiceImpl implements StoreAreaService {
  @Autowired
  StoreAreaMapper storeAreaMapper;
  @Autowired
  StoreMapper storeMapper;

    @Override
    public List<Map<String, Object>> querySA() {
      // 调用 Mapper 层的 selectList 方法（返回多条记录）
      return storeAreaMapper.querySA();
    }

  //更新
  @Override
  public int updaSA(Integer warmArea,Integer ckName,String kuName,String beizhu,Integer id) {

    return storeAreaMapper.updaSA(warmArea,ckName,kuName,beizhu,id);
  }

// 分页功能  太low
  @Override
  public PageBean<Kuqu> fenyeSA(int page, int pageSize) {
    //校验参数
    if (page<1){
      page=1;
    }
    if (pageSize<1 || pageSize>100){
      pageSize=10;
    }
    // 计算偏移量（关键！分页必须用 offset = (page-1)*pageSize）
    int offset = (page - 1) * pageSize;
    List<Kuqu> list=storeAreaMapper.selectByPage(offset,pageSize);
    long total=storeAreaMapper.selectTotalCount();
    //封装数据
    PageBean newPB=new PageBean<>();
    newPB.setPage(page);
    newPB.setPageSize(pageSize);
    newPB.setTotal(total);
    newPB.setList(list);

    return newPB;
  }


  //条件查询+分页
  @Override
  public PageBean<Kuqu> chaByKu(int page, int pageSize, String ckName, String kuName) {
    PageHelper.startPage(page,pageSize);
    List<Kuqu> list=storeAreaMapper.chaByKu(ckName,kuName);
    PageInfo info=new PageInfo<>(list);
    return new PageBean(page,pageSize,info.getTotal(),list);

  }


  @Override
  public int addKu(Warehouse warehouse) {
    return storeAreaMapper.addKu(warehouse);
  }

  //增加库区
  @Override
  @Transactional(rollbackFor = Exception.class) // 添加事务
  public ResultBean addWarehouse(WarehouseAddDTO dto) {


      Warehouse warehouse=new Warehouse();
      warehouse.setCkid(dto.getCkName());
      warehouse.setWareid(dto.getWarmAreaName());
      warehouse.setName(dto.getKuname());
      warehouse.setBeizhu(dto.getBeizhu());

    int n =storeAreaMapper.addKu(warehouse);
    return R.success("增加",n);
  }

  //根据id删除
  @Override
  public int deleKu(int id) {
    return storeAreaMapper.deleKu(id);
  }
}
